package com.sinba.common.system.dao;

import com.sinba.common.dao.BaseDAOImplHibernate;
import com.sinba.common.dto.PageDTO;
import com.sinba.common.system.dto.RuleDTO;
import com.sinba.common.system.entity.Rule;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import org.springframework.util.StringUtils;

import java.util.List;

public class RuleDAO
    extends BaseDAOImplHibernate<Rule>
    implements IRuleDAO
{
    public PageDTO findRulePager( RuleDTO qdto, String sord, String sidx )
    {
        final DetachedCriteria dc = DetachedCriteria.forClass( Rule.class );

        if ( qdto != null )
        {
            if ( StringUtils.hasText( qdto.getRuleName(  ) ) )
            {
                dc.add( Restrictions.like( "ruleName",
                                           qdto.getRuleName(  ),
                                           MatchMode.ANYWHERE ) );
            }

            if ( StringUtils.hasText( qdto.getRuleCode(  ) ) )
            {
                dc.add( Restrictions.like( "ruleCode",
                                           qdto.getRuleCode(  ),
                                           MatchMode.ANYWHERE ) );
            }
        }

        //����
        if ( ( sord != null ) && ! sord.equals( "" ) && ( sidx != null ) && ! sidx.equals( "" ) )
        {
            if ( sord.equals( "desc" ) )
            {
                dc.addOrder( Order.desc( sidx ) );
            } else{
                dc.addOrder( Order.asc( sidx ) );
            }
        }

        return super.findPageByCriteria( dc,
                                         qdto.getStart(  ),
                                         qdto.getLimit(  ) );
    }
    ;

    /**
     * 根据规则查询
     * @param code
     * @return
     */
    public List<Rule> findRuleByCode( String[] code )
    {
        final DetachedCriteria dc = DetachedCriteria.forClass( Rule.class );
        dc.add( Restrictions.in( "ruleCode", code ) );

        return super.getHibernateTemplate(  ).findByCriteria( dc );
    }
}
